<template>
  <div>
    <div class="top-box">
      <router-link to="/admin/news/special/author/detail"><el-button type="success">添加作者</el-button></router-link>
      <router-link to="/admin/news/editor"><el-button type="success">发布新闻</el-button></router-link>
      <!--<el-button type="success" @click="add">添加作者</el-button>-->
    </div>
    <loading v-if="loading"></loading>
    <div class="pd" v-else>
      <div class="search-box">
        <el-input v-model="searchParam.title" placeholder="请输入作者名称" class="user-input"></el-input>
        <el-select v-model="searchParam.is_top" placeholder="请选择是否置顶" class="user-input">
          <el-option v-for="item in topOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
			  <el-select v-model="searchParam.is_recommend" placeholder="请选择是否推荐" class="user-input">
          <el-option v-for="item in recommendOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
			  <el-select v-model="searchParam.status" placeholder="请选择是否发布" class="user-input">
          <el-option v-for="item in statusOptions"
            :key="item.id"
            :label="item.name"
            :value="item.id">
          </el-option>
        </el-select>
        <el-button type="primary" class="el-icon-search" @click="search">搜索</el-button>
        <el-button class="el-icon-q" @click="cancel">撤销</el-button>
      </div>

      <el-table :data="listData.list" border fit highlight-current-row style="width:100%">
        <el-table-column label="编号" prop="id"></el-table-column>
        <el-table-column label="作者头像">
          <template scope="scope">
            <img :src="scope.row.cover_path" class="art-cover" @click="previewImg(scope.row.cover_path)">
          </template>
        </el-table-column>
        <el-table-column label="一级分类" prop="first_level_cname"></el-table-column>
        <el-table-column label="二级分类" prop="second_level_cname"></el-table-column>
        <el-table-column label="作者名称" prop="title"></el-table-column>
        <el-table-column label="作者简介" prop="note"></el-table-column>
        <!--<el-table-column label="是否置顶">
          <template scope="scope">
            <div class="input-group-box">
              <el-switch
                v-model="scope.row.is_top"
                on-text="是"
                off-text="否"
                @change="changeTop($event, scope.row.id, scope.row.title, scope.$index)">
              </el-switch>
            </div>
          </template>
        </el-table-column>-->
        <el-table-column label="是否推荐">
          <template scope="scope">
            <div class="input-group-box">
              <el-switch
                v-model="scope.row.is_recommend"
                on-text="是"
                off-text="否"
                @change="changeRecommend($event, scope.row.id, scope.row.title, scope.$index)">
              </el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="是否发布">
          <template scope="scope">
            <div class="input-group-box">
              <el-switch
                v-model="scope.row.status"
                on-text="是"
                off-text="否"
                @change="changePublish($event, scope.row.id, scope.row.title, scope.$index)">
              </el-switch>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="发布时间" prop="repub_time"></el-table-column>
        <el-table-column label="排序值" prop="sort"></el-table-column>
        <el-table-column label="操作">
          <template scope="scope">
            <i class="i-icon icon-revamp" title="修改" @click="edit(scope.row.id)"></i>
            <i class="i-icon icon-delete" title="删除"  @click="remove(scope.row.id)"></i>
          </template>
        </el-table-column>
      </el-table>
      <div class="pagination-container">
        <el-pagination
          @current-change="handleChange"
          :current-page.sync="pageall"
          :page-size="pageSize"
          layout="total, prev, pager, next, jumper"
          :total="listData.total">
        </el-pagination>
      </div>
      <el-dialog
        title="查看作者头像"
        :visible.sync="imgVisible"
        :before-close="handleClose"
        size="tiny">
        <img :src="imgUrl" style="width:100%">
      </el-dialog>
      <div class="art-fade" v-if="loadPreview">
        <load></load>
      </div>
    </div>
    <el-dialog title="添加作者" :visible.sync="showAddModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>一级分类：</label>
        <div class="input-group-box"><el-input v-model="addParam.first_level_cid" placeholder="请输入一级分类"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>二级分类：</label>
        <div class="input-group-box"><el-input v-model="addParam.second_level_cid" placeholder="请输入二级分类"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>标题：</label>
        <div class="input-group-box"><el-input v-model="addParam.title" placeholder="请输入标题"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>描述：</label>
        <div class="input-group-box"><el-input v-model="addParam.note" placeholder="请输入描述"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>详细描述：</label>
        <div class="input-group-box"><el-input v-model="addParam.content" placeholder="请输入详细描述"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>是否置顶：</label>
        <div class="input-group-box"><el-input v-model="addParam.is_top" placeholder="请输入是否置顶"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>是否推荐：</label>
        <div class="input-group-box"><el-input v-model="addParam.is_recommend" placeholder="请输入是否推荐"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>排序值：</label>
        <div class="input-group-box"><el-input v-model="addParam.sort" placeholder="请输入排序值"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>重新发布时间：</label>
        <div class="input-group-box"><el-input v-model="addParam.repub_time" placeholder="请输入重新发布时间"></el-input></div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="showAddModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="confirmAdd">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="修改作者" :visible.sync="showUpdateModelFlag" size="tiny">
      <div class="input-group">
        <label><i>*</i>一级分类：</label>
        <div class="input-group-box"><el-input v-model="editParam.first_level_cid" placeholder="请输入一级分类"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>二级分类：</label>
        <div class="input-group-box"><el-input v-model="editParam.second_level_cid" placeholder="请输入二级分类"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>标题：</label>
        <div class="input-group-box"><el-input v-model="editParam.title" placeholder="请输入标题"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>描述：</label>
        <div class="input-group-box"><el-input v-model="editParam.note" placeholder="请输入描述"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>详细描述：</label>
        <div class="input-group-box"><el-input v-model="editParam.content" placeholder="请输入详细描述"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>是否置顶：</label>
        <div class="input-group-box"><el-input v-model="editParam.is_top" placeholder="请输入是否置顶"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>是否推荐：</label>
        <div class="input-group-box"><el-input v-model="editParam.is_recommend" placeholder="请输入是否推荐"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>排序值：</label>
        <div class="input-group-box"><el-input v-model="editParam.sort" placeholder="请输入排序值"></el-input></div>
      </div>
	    <div class="input-group">
        <label><i>*</i>重新发布时间：</label>
        <div class="input-group-box"><el-input v-model="editParam.repub_time" placeholder="请输入重新发布时间"></el-input></div>
      </div>

      <div slot="footer" class="dialog-footer">
        <el-button @click="showUpdateModelFlag = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import apiConfig from '../../utils/apiconfig'
import api from '../../utils/api'
import helper from '../../utils/helper'
import loading from '../../components/loading'
export default {
  data () {
    return {
      listData: [],
      pageSize: 20,
      pageall: 1,
      page: 1,
      loading: true,
      showAddModelFlag: false,
      showUpdateModelFlag: false,
      recommendOptions: [
        {
          id: '',
          name: '全部推荐状态'
        },
        {
          id: 1,
          name: '已推荐'
        },
        {
          id: 0,
          name: '未推荐'
        }
      ],
      topOptions: [
        {
          id: '',
          name: '全部置顶状态'
        },
        {
          id: 1,
          name: '已置顶'
        },
        {
          id: 0,
          name: '未置顶'
        }
      ],
      statusOptions: [
        {
          id: '',
          name: '全部发布状态'
        },
        {
          id: 1,
          name: '已发布'
        },
        {
          id: 0,
          name: '未发布'
        }
      ],
      searchParam: {
        id: '',
        first_level_cid: '',
        second_level_cid: '',
        title: '',
        is_top: '',
        is_recommend: ''
      },
      addParam: {
        first_level_cid: '',
        second_level_cid: '',
        title: '',
        note: '',
        content: '',
        is_top: '',
        is_recommend: '',
        sort: '',
        repub_time: ''
      },
      editParam: {
        id: '',
        first_level_cid: '',
        second_level_cid: '',
        title: '',
        note: '',
        content: '',
        is_top: '',
        is_recommend: '',
        sort: '',
        repub_time: ''
      },
      imgVisible: false,
      imgUrl: '',
      isPreview: false,
      loadPreview: false
    }
  },
  created () {
    this.getList()
  },
  components: {
    loading
  },
  methods: {
    getSearchParam () { // 参数
      var param = {
        page: this.page,
        id: this.searchParam.id,
        first_level_cid: this.searchParam.first_level_cid,
        second_level_cid: this.searchParam.second_level_cid,
        title: this.searchParam.title,
        is_top: this.searchParam.is_top,
        is_recommend: this.searchParam.is_recommend
      }
      this.params = param
    },
    getList () {
      this.getSearchParam()
      api.GET(helper.getManageApiUrl(apiConfig.specialAuthorList), this.params, res => {
        this.listData = res
        var list = this.listData.list
        list.forEach((item, index) => {
          if (item.is_top === 0) {
            item.is_top = false
          } else {
            item.is_top = true
          }
          if (item.is_recommend === 0) {
            item.is_recommend = false
          } else {
            item.is_recommend = true
          }
          if (item.status === 0) {
            item.status = false
          } else {
            item.status = true
          }
        })
        this.loading = false
      }, res => {
        console.log(res)
      })
    },
    search () {
      this.getList()
    },
    cancel () {
      this.searchParam.first_level_cid = ''
      this.searchParam.second_level_cid = ''
      this.searchParam.title = ''
      this.searchParam.note = ''
      this.searchParam.content = ''
      this.searchParam.is_top = ''
      this.searchParam.is_recommend = ''
      this.searchParam.sort = ''
      this.searchParam.repub_time = ''
    },
    handleChange (val) {
      this.page = val
      this.getList()
    },
    add () {
      this.showAddModelFlag = true
    },
    confirmAdd () {
      var param = {
        first_level_cid: this.addParam.first_level_cid,
        second_level_cid: this.addParam.second_level_cid,
        title: this.addParam.title,
        note: this.addParam.note,
        content: this.addParam.content,
        is_top: this.addParam.is_top,
        is_recommend: this.addParam.is_recommend,
        sort: this.addParam.sort,
        repub_time: this.addParam.repub_time
      }
      api.GET(helper.getManageApiUrl(apiConfig.addSpecialAuthor), param, res => {
        this.$notify({
          title: '成功',
          message: '添加成功',
          type: 'success',
          duration: 1500
        })
        this.addParam.first_level_cid = ''
        this.addParam.second_level_cid = ''
        this.addParam.title = ''
        this.addParam.note = ''
        this.addParam.content = ''
        this.addParam.is_top = ''
        this.addParam.is_recommend = ''
        this.addParam.sort = ''
        this.addParam.repub_time = ''
        this.showAddModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: res.msg,
          type: 'fail',
          duration: 1500
        })
      })
    },
    remove (id) {
      var param = {
        id: id
      }
      this.$confirm('确认要删除此条信息吗？')
      .then(_ => {
        api.GET(helper.getManageApiUrl(apiConfig.deleteSpecialAuthor), param, res => {
          this.$notify({
            title: '成功',
            message: '删除成功',
            type: 'success',
            duration: 1500
          })
          this.getList()
        }, res => {
          this.$notify({
            title: '失败',
            message: '删除失败',
            type: 'error',
            duration: 1500
          })
        })
      })
      .catch(_ => {})
    },
    edit (id) { // 修改文章
//      var secondLevelCateList = JSON.stringify(this.secondLevelCategoryList)
//      var sparam = crypt.encrypt(secondLevelCateList)
//      window.sessionStorage.setItem('secondLevelCategoryList', sparam)
//      let paramjson = JSON.stringify(this.params)
//      let sparams = crypt.encrypt(paramjson)
//      window.sessionStorage.setItem('params', sparams)
      let url = '/admin/news/special/author/detail?id=' + id
      this.$router.push(url)
    },
    save () {
      api.GET(helper.getManageApiUrl(apiConfig.updateSpecialAuthor), this.editParam, res => {
        this.$notify({
          title: '成功',
          message: '修改成功',
          type: 'success',
          duration: 1500
        })
        this.showUpdateModelFlag = false
        this.getList()
      }, res => {
        this.$notify({
          title: '失败',
          message: '修改失败',
          type: 'error',
          duration: 1500
        })
      })
    },
    previewImg (cover) {
      this.imgUrl = cover
      this.imgVisible = true
    },
    changeTop (event, id, title, index) {
      var tip = event === true ? '确定将作者“' + title + '”置顶吗' : '确定取消置顶作者“' + title + '”吗？'
      var param = {
        id: id,
        is_top: event === true ? 1 : 0
      }
      this.changeStatus(param, tip, () => {
        this.listData.list[index].is_top = !event
      })
    },
    changeRecommend (event, id, title, index) {
      var tip = event === true ? '确定将作者“' + title + '”设置为为推荐吗' : '确定取消作者“' + title + '”的推荐吗？'
      var param = {
        id: id,
        is_recommend: event === true ? 1 : 0
      }
      this.changeStatus(param, tip, () => {
        this.listData.list[index].is_recommend = !event
      })
    },
    changePublish (event, id, title, index) {
      var tip = event === true ? '确定将作者“' + title + '”发布吗' : '确定取消发布作者“' + title + '”吗？'
      var param = {
        id: id,
        status: event === true ? 1 : 0
      }
      this.changeStatus(param, tip, () => {
        this.listData.list[index].status = !event
      })
    },
    changeStatus (param, tip, fn) {
      this.$confirm(tip)
        .then(_ => {
          api.GET(helper.getManageApiUrl(apiConfig.swithUpdateAuthor), param, res => {
            this.$notify({
              title: '成功',
              message: '操作成功',
              type: 'success',
              duration: 1500
            })
            this.getlist()
          }, res => {
            this.$notify({
              title: '失败',
              message: res.msg,
              type: 'error',
              duration: 1500
            })
          })
        }).catch(_ => {
          typeof fn === 'function' && fn()
        })
    }
  }
}
</script>
<style>
 .search-box {
  font-size: 0;
  padding-bottom:10px;
 }
 .search-box .user-input{
   width: 197px;
   margin: 3px 2px;
 }
 .art-cover {
   width: 95px;
   height: 70px;
 }
 .art-fade {
   position: fixed;
   top: 0;
   left: 0;
   width: 100%;
   height: 100%;
   z-index: 99;
   background: rgba(255, 255, 255, 0.7);
   padding-top: 5%;
 }
</style>

